<?php
//validate install
if(!class_exists('ADS_Bootstrapper')){
    die('Plugin ADS Server is not installed.');
}
require_once __DIR__.'/php/ClientConnection.php';
//time zone
date_default_timezone_set('Europe/Prague');
//execute
switch (ADS_Input::getVar('do','default')) {
    
    case 'callPipeline':
        //load pipeline
        require_once __DIR__.'/php/formats/Format.php';
        require_once __DIR__.'/php/formats/CNBReader.php';
        require_once __DIR__.'/php/pipelines/ContentPipeline.php';        
        //use pipeline
        $pipeline = new ADS_ContentPipeline();
        $pipeline->registerFormat('cnb.cz', new ADS_CNBReader());
        //weather service
        $pipeline->registerService('worldweatheronline.com:simple', array(
            'url'   => 'http://free.worldweatheronline.com/feed/weather.ashx',
            'query' => array(
                'q'           => 'Praha,Czech+Republic',
                'format'      => 'json',
                'num_of_days' => '2',
                'key'         => '62805a27d3114458111509'
            )
        ), 'json');
        $pipeline->registerService('worldweatheronline.com:complete', array(
            'url'   => 'http://free.worldweatheronline.com/feed/weather.ashx',
            'query' => array(
                'q'           => 'Reykjavik,Iceland',
                'format'      => 'json',
                'num_of_days' => '5',
                'key'         => '62805a27d3114458111509'
            )
        ), 'json');
        //exchange rate service
        $pipeline->registerService('cnb.cz:today', array(
            'url'   => 'http://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt',
            'query' => array(
                'date' => date('d.m.Y')
            )
        ), 'cnb.cz');
        $pipeline->registerService('cnb.cz:yesterday', array(
            'url'   => 'http://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt',
            'query' => array(
                'date' => date('d.m.Y',time()-24*3600)
            )
        ), 'cnb.cz');
        //rss service
        $pipeline->registerService('idnes.cz', array(
            'url'    => 'http://servis.idnes.cz/rss.asp',
            'format' => 'xml'
        ), 'xml');
        $pipeline->toJSON(ADS_Input::getVar('service','unknown'));      
        break;
    
    case 'sendDescriptor':
        try {
            $transmitter = new ADS_Transmitter();
            $transmitter->listen(new ADS_ClientConnection(
                ADS_Input::getVar('ckey','unknown'),
                ADS_Input::getVar('sw','unknown')
            ), is_user_logged_in());
            $transmitter->sendDescriptor('json');
        } catch (ADS_InvalidKeyException $exc) {
            echo $exc->getTraceAsString();
        }        
        break;   
    
    case 'sendCurrentRevision':
        try {
            $transmitter = new ADS_Transmitter();
            $transmitter->listen(new ADS_ClientConnection(
                ADS_Input::getVar('ckey','unknown'),
                ADS_Input::getVar('sw','unknown')
            ), is_user_logged_in());
            $transmitter->sendCurrentRevision();
        } catch (ADS_InvalidKeyException $exc) {
            echo $exc->getTraceAsString();
        }        
        break; 
    
    case 'sendCommands':
        try {
            $transmitter = new ADS_Transmitter();
            $transmitter->listen(new ADS_ClientConnection(
                ADS_Input::getVar('ckey','unknown'),
                ADS_Input::getVar('sw','unknown')
            ), is_user_logged_in());
            $transmitter->sendCommands();
        } catch (ADS_InvalidKeyException $exc) {
            echo $exc->getTraceAsString();
        }        
        break; 
    
    case 'confirmCommands':
        try {
            $transmitter = new ADS_Transmitter();
            $transmitter->listen(new ADS_ClientConnection(
                ADS_Input::getVar('ckey','unknown'),
                ADS_Input::getVar('sw','unknown')
            ), is_user_logged_in());
            $transmitter->confirmCommands();
        } catch (ADS_InvalidKeyException $exc) {
            echo $exc->getTraceAsString();
        }        
        break; 
    
    default:
        require_once __DIR__.'/preloader.php';
        break;        
}

